package com.example.mobifin_busticket.models;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.JSONConstants;

@SuppressWarnings("serial")
public class AllsourcesInfoEntity implements Serializable {
    private String sourceId = "", sourceName = "", responsesourceDesc = "";

    public String getResponseDesc() {
        return responsesourceDesc;
    }

    public void setResponseDesc(String responseDesc) {
        this.responsesourceDesc = responseDesc;
    }

    public String getId() {
        return sourceId;
    }

    public void setId(String id) {
        this.sourceId = id;
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public static List<AllsourcesInfoEntity> parse(String json) {
        Log.e("json", "" + json);
        List<AllsourcesInfoEntity> allSourcesInfoEntity = new ArrayList<AllsourcesInfoEntity>();
        AllsourcesInfoEntity mAllsourcesInfoEntity = null;
        JSONObject root;
        try {
            if (json != null && json.length() > 0) {
                if (json.toString().equalsIgnoreCase(AppData.INTERNELERROR)) {
                    mAllsourcesInfoEntity = new AllsourcesInfoEntity();
                    mAllsourcesInfoEntity
                            .setResponseDesc(AppData.INTERNELERROR);
                    allSourcesInfoEntity.add(mAllsourcesInfoEntity);
                } else {
                    root = new JSONObject(json);
                    JSONArray mJsonArray = root
                            .getJSONArray(JSONConstants.DstGetAllCity.CITY);
                    Log.e("mJsonArray", "" + mJsonArray.length());
                    ifnotNullCity(root, mJsonArray, mAllsourcesInfoEntity,
                            allSourcesInfoEntity);
                }
            } else {
                mAllsourcesInfoEntity = new AllsourcesInfoEntity();
                mAllsourcesInfoEntity.setResponseDesc(AppData.INTERNELERROR);
                allSourcesInfoEntity.add(mAllsourcesInfoEntity);
            }
        } catch (JSONException e) {
            mAllsourcesInfoEntity = new AllsourcesInfoEntity();
            mAllsourcesInfoEntity.setResponseDesc(AppData.INTERNELERROR);
            allSourcesInfoEntity.add(mAllsourcesInfoEntity);
            Log.e(JSONConstants.EXCEPTION, e.toString(), e);
        }
        return allSourcesInfoEntity;
    }

    private static void ifnotNullCity(JSONObject root, JSONArray mJsonArray,
            AllsourcesInfoEntity mAllsourcesInfoEntity,
            List<AllsourcesInfoEntity> allSourcesInfoEntity) {

        try {
            if (root.optJSONArray(JSONConstants.DstGetAllCity.CITY) instanceof JSONArray) {
                forloopfAllcity(mJsonArray, mAllsourcesInfoEntity,
                        allSourcesInfoEntity);
            } else if (root.optJSONArray(JSONConstants.DstGetAllCity.CITY) == null) {
                JSONObject city = root
                        .getJSONObject(JSONConstants.DstGetAllCity.CITY);
                forloopfrsingleCity(city, mAllsourcesInfoEntity,
                        allSourcesInfoEntity);
            }
        } catch (Exception e) {
            Log.e(JSONConstants.EXCEPTION, e.toString(), e);
        }
    }

    private static void forloopfrsingleCity(JSONObject city,
            AllsourcesInfoEntity mAllsourcesInfoEntity,
            List<AllsourcesInfoEntity> allSourcesInfoEntity) {
        AllsourcesInfoEntity mAllsourcesInfoEntityPass = mAllsourcesInfoEntity;
        mAllsourcesInfoEntityPass = new AllsourcesInfoEntity();
        try {
            mAllsourcesInfoEntityPass.setId(city
                    .getString(JSONConstants.DstGetAllCity.ID));
            mAllsourcesInfoEntityPass.setSourceName(city
                    .getString(JSONConstants.DstGetAllCity.CITY_NAME));
            allSourcesInfoEntity.add(mAllsourcesInfoEntityPass);
        } catch (JSONException e) {
            Log.e(JSONConstants.EXCEPTION, e.toString(), e);
        }
    }

    private static void forloopfAllcity(JSONArray mJsonArray,
            AllsourcesInfoEntity mAllsourcesInfoEntity,
            List<AllsourcesInfoEntity> allSourcesInfoEntity) {
        AllsourcesInfoEntity mAllsourcesInfoEntitypass = mAllsourcesInfoEntity;
        List<AllsourcesInfoEntity> allSourcesInfoEntitypass = allSourcesInfoEntity;
        for (int i = 0; i < mJsonArray.length(); i++) {
            JSONObject c;
            try {
                c = mJsonArray.getJSONObject(i);
                mAllsourcesInfoEntitypass = new AllsourcesInfoEntity();
                mAllsourcesInfoEntitypass.setId(c
                        .getString(JSONConstants.DstGetAllCity.ID));
                mAllsourcesInfoEntitypass.setSourceName(c
                        .getString(JSONConstants.DstGetAllCity.CITY_NAME));
                allSourcesInfoEntitypass.add(mAllsourcesInfoEntitypass);
            } catch (JSONException e) {
                Log.e(JSONConstants.EXCEPTION, e.toString(), e);
            }
        }
    }
}
